package com.myzl.stream.service.impl;

import com.myzl.common.config.VideoConfig;
import com.myzl.common.constant.HttpStatus;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.stream.pojo.ImageData;
import com.myzl.stream.service.IHIKSDKService;
import com.myzl.stream.util.hc.CameraCache;
import com.myzl.stream.util.hc.HCNetSDK;
import com.myzl.stream.util.hc.HCSDKUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class HIKSDKServiceImpl implements IHIKSDKService {

    private static HCNetSDK hCNetSDK = null;


    @Override
    public AjaxResult saveImage(ImageData imageData) {
        try{
            if (hCNetSDK == null) {
                hCNetSDK = HCSDKUtil.createSDKInstance(VideoConfig.getDllPath(),hCNetSDK);
                HCSDKUtil.loadSdk(VideoConfig.getDllPath(),hCNetSDK);
                HCSDKUtil.initHCCamera(hCNetSDK);
            }
            if (hCNetSDK == null) {
                return AjaxResult.error("SDK未初始化");
            }
            int lUserID = CameraCache.getUserId(imageData.getDeviceId());
            if (!HCSDKUtil.getWorkState(hCNetSDK,lUserID)){
                // 注销后登录
                HCSDKUtil.loginOut(hCNetSDK,lUserID);
                //尝试登录
                //String ip = "10.10.10.200",username = "admin",password="hc@123456";
                //short port = (short)8000;
                lUserID = HCSDKUtil.login(hCNetSDK, imageData.getIp(), imageData.getPort(), imageData.getUsername(), imageData.getPassword());
                if (lUserID == -1) {
                    return AjaxResult.error(HttpStatus.LOGIN_FAILED,"登录失败");
                } else {
                    CameraCache.setUserId(imageData.getDeviceId(),lUserID);
                }
            }

            if (!HCSDKUtil.getWorkState(hCNetSDK,lUserID)){
                return AjaxResult.error("获取设备能力集失败");
            }
            int pictureCount = 2;
            boolean pictureResult = false;
            //多次抓图，暂定抓两次
            for (int i = 0; i < pictureCount; i++) {
                //按照每天设置一个目录
                String fileNamePath = HCSDKUtil.getFileName(VideoConfig.getPicPath(),imageData.getDeviceId());
                if (HCSDKUtil.getPicture(hCNetSDK,lUserID,imageData.getChannel(),fileNamePath)){
                    pictureResult = true;
                }
            }
            if (!pictureResult){
                return AjaxResult.error("抓取图片失败");
            }

            return AjaxResult.success("抓取图片成功");
        }catch (Exception e){
            log.error("截图出现异常",e);
            return AjaxResult.error("抓取图片失败");
        }
    }

    @Override
    public AjaxResult cameraStatus(ImageData imageData) {
        int lUserID = -1;
        try{
            if (hCNetSDK == null) {
                hCNetSDK = HCSDKUtil.createSDKInstance(VideoConfig.getDllPath(),hCNetSDK);
                HCSDKUtil.loadSdk(VideoConfig.getDllPath(),hCNetSDK);
                HCSDKUtil.initHCCamera(hCNetSDK);
            }
            if (hCNetSDK == null) {
                return AjaxResult.error("SDK未初始化");
            }
            lUserID = HCSDKUtil.login(hCNetSDK, imageData.getIp(), imageData.getPort(), imageData.getUsername(), imageData.getPassword());
            if (lUserID == -1) {
                return AjaxResult.error(HttpStatus.LOGIN_FAILED,"登录失败");
            }
            return AjaxResult.success("登录片成功");
        }catch (Exception e){
            log.error("登录出现异常",e);
            return AjaxResult.error("登录异常");
        } finally {
            HCSDKUtil.loginOut(hCNetSDK,lUserID);
        }
    }

    @Override
    public AjaxResult aiSaveImage(ImageData imageData) {
        try{
            if (hCNetSDK == null) {
                hCNetSDK = HCSDKUtil.createSDKInstance(VideoConfig.getDllPath(),hCNetSDK);
                HCSDKUtil.loadSdk(VideoConfig.getDllPath(),hCNetSDK);
                HCSDKUtil.initHCCamera(hCNetSDK);
            }
            if (hCNetSDK == null) {
                return AjaxResult.error("SDK未初始化");
            }
            int lUserID = CameraCache.getUserId(imageData.getDeviceId());
            if (!HCSDKUtil.getWorkState(hCNetSDK,lUserID)){
                // 注销后登录
                HCSDKUtil.loginOut(hCNetSDK,lUserID);
                //尝试登录
                //String ip = "10.10.10.200",username = "admin",password="hc@123456";
                //short port = (short)8000;
                lUserID = HCSDKUtil.login(hCNetSDK, imageData.getIp(), imageData.getPort(), imageData.getUsername(), imageData.getPassword());
                if (lUserID == -1) {
                    return AjaxResult.error(HttpStatus.LOGIN_FAILED,"登录失败");
                } else {
                    CameraCache.setUserId(imageData.getDeviceId(),lUserID);
                }
            }

            if (!HCSDKUtil.getWorkState(hCNetSDK,lUserID)){
                return AjaxResult.error("获取设备能力集失败");
            }

            //按照每天设置一个目录
            String fileName = HCSDKUtil.getAiFileName(VideoConfig.getAiPath(),imageData.getDeviceId());
            boolean pictureResult = HCSDKUtil.getPicture(hCNetSDK,lUserID,imageData.getChannel(),fileName);
            if (!pictureResult){
                return AjaxResult.error("抓取图片失败");
            }

            return AjaxResult.success("抓取图片成功",fileName);
        }catch (Exception e){
            log.error("截图出现异常",e);
            return AjaxResult.error("抓取图片失败");
        }
    }


}
